package com.cms.work.service.impl.sys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.work.core.service.impl.BaseServiceImpl;
import com.cms.work.dao.sys.ISysUserRoleDao;
import com.cms.work.entity.sys.SysUserRole;
import com.cms.work.service.sys.ISysUserRoleService;

/**
 * @describe 用户角色关联模块
 * @author YY
 * @version v1.0
 * @date 2016-11-22 14:48:23
 **/
@Service
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRole, Integer> implements ISysUserRoleService {
	
    @Autowired
    private ISysUserRoleDao sysUserRoleDao;
	
	@Override
	public void save(SysUserRole sysUserRole) {
	    if (sysUserRole.getId() == null || sysUserRole.getId() == 0) {
            sysUserRoleDao.insertSelective(sysUserRole);
        } else {
            sysUserRoleDao.updateByPrimaryKeySelective(sysUserRole);
        }
	}
	
	@Override
    public void addUserAndRoleLink(Integer userId, Integer[] roles) {
        List<SysUserRole> userRoleList = new ArrayList<SysUserRole>();
        if(roles != null && roles.length > 0){
        	sysUserRoleDao.deleteByUserId(userId);
            for (int i = 0; i < roles.length; i++) {
                SysUserRole userRole = new SysUserRole(userId, roles[i]);
                userRoleList.add(userRole);
            }
            sysUserRoleDao.batchInsertUserRole(userRoleList);
        }
    }

}

